
global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/tab_A25_numbers.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"

capture noi {

cap log close tabl

log using ${numb_dir}/Table_A25_mp_outliers_numbers.log, replace name(num)

* Other stats provided in the paper on the MP comparison
*only available with proprietary data from patstat and MP
use ${final_dir}/MP_DHOZ_ipc_comparison.dta, clear
keep if total >= 100 & total_MP_patent >= 100
gen diff_sh = share_anyclassification - share_auto_puettmann

gen ipc4 = substr(ipc6xx, 1, 4)
gen in_subclass = 1 if inlist(ipc4, "B66B", "B41J", "B65H")
list ipc6xx share_anyclassification in_subclass, sep(6) table abb(25)

use ${final_dir}/MP_DHOZ_common_data.dta, clear
mmerge appln_id using ${commondata_dir}/patstat_2018b/family_info.dta, unmatched(master) ukeep(docdb_family_id)
drop _m
gen pat = 1
bys docdb: gen pats_per_docdbd = sum(pat)
bys docdb: gen automats_per_docdb = sum(automat_puettmann)
bys docdb: gen fraction_automat_pm = automats_per_docdb / pats_per_docdbd
gen docdb_is_split = 0 
replace docdb_is_split = 1 if fraction_automat_pm > 0 & fraction_automat_pm < 1
tab docdb_is_split if fraction_automat_pm > 0

cap log close num

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat